package com.lemon.smartsafety.ui.activity.safe.data;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.common.SelectDataI;
import com.lemon.smartsafety.entity.event.datacoll.BuildListItemEvent;
import com.lemon.smartsafety.entity.event.datacoll.KeyWordEvent;
import com.lemon.smartsafety.entity.event.datacoll.PopupEvent;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.SafeDataServices;
import com.lemon.smartsafetynew.ui.activity.base.BaseSearchFragmentActivity;
import com.lemon.smartsafety.ui.fragment.safe.data.BuildingListFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Cooliean on 16/1/24.
 * 安全巡查 建筑管理界面
 */
public class SafeDataCollectionBuildingListActivity extends BaseSearchFragmentActivity {
    private LoggerHelper loggerHelper = new LoggerHelper(SafeDataCollectionBuildingListActivity.class);

    private BuildingListFragment mFragment;
    private CompanyInfo          mCompanyInfo;


    private     List<SelectDataI> mGroupItems = new ArrayList<>();
    private String            mKeyWord    = "";


    private boolean isHold=false;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            mCompanyInfo = (CompanyInfo) getIntent().getSerializableExtra(BundleKey.COMPANY_INFO);
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        mFab.setVisibility(View.VISIBLE);
        mFab.setDrawableIcon(getResources().getDrawable(R.mipmap.ic_add_24px));
        mFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                if (null != mCompanyInfo) {
                    bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
                }
                UIHelper.showSafeDataCollectionBuildingProp(SafeDataCollectionBuildingListActivity.this, bundle);
            }
        });
    }

    @Override
    public Fragment getFragment() {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
        mFragment = BuildingListFragment.newInstance();
        mFragment.setArguments(bundle);
        mKeyWord = "";
        return mFragment;
    }


    public Fragment getFragment(String text) {
        Bundle bundle = new Bundle();
        bundle.putString(BundleKey.KEY_WORD, text);
        bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
        mFragment = BuildingListFragment.newInstance();
        mFragment.setArguments(bundle);
        mKeyWord = StringUtils.nullValue(text);
        return mFragment;
    }

    @Override
    public int getSearchHintRes() {
        return R.string.search_common_hit;
    }

    @Override
    protected void initTitle() {
        this.setBarTitle(R.string.safe_data_building_list);
    }

    @Override
    public boolean onQueryTextSubmit(String queryText) {
        queryText = StringUtils.nullValue(queryText);
        getSupportFragmentManager().beginTransaction()
                                   .replace(R.id.main_content, getFragment(queryText), getFragment(queryText).getClass()
                                                                                                             .getName())
                                   .commit();
        return super.onQueryTextSubmit(queryText);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        setIconEnable(menu, true);
        boolean menuResult = super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_filter, menu);
        return menuResult;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_filter) {
            showPopWindow();
        }
        return super.onOptionsItemSelected(item);
    }


    public void showPopWindow(){
        if (mGroupItems.isEmpty()) {
            SafeDataServices.init().getBuildingGroupList();
        }else{
            postPopEvent();
        }
    }

    @Subscribe
    public void onEventMainThread(HttpCallbackEvent event) {
        if (EventTag.BUILDING_GROUP_LIST.equals(event.getTag())) {
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    List<SelectDataI> dataIs = (List<SelectDataI>) event.getObj();
                    if (null != dataIs && dataIs.size() != 0) {
                        mGroupItems.clear();
                        mGroupItems.addAll(dataIs);
                    }
                }
                postPopEvent();
            }
        }
    }

    private void postPopEvent(){
        if(!isHold) {
            PopupEvent appEvent = new PopupEvent();
            appEvent.setKeyword(mKeyWord);
            appEvent.setData(mGroupItems);
            AppService.getBus().post(appEvent);
        }
    }

    @Subscribe
    public void onEventMainThread(KeyWordEvent event) {
        mGroupItems.clear();
        mGroupItems.addAll(event.getData());
        mKeyWord = StringUtils.nullValue(event.getKeyword());
    }

    @Subscribe
    public void onEventMainThread(BuildListItemEvent event) {
        if (BuildingListFragment.class.getName().equals(event.getTag())) {
            if (mCompanyInfo == null) {
                mHUDUtil.showErrorTip(getString(R.string.params_error));
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.COMPANY_BUILD_INFO, event.getData());
            bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
            UIHelper.showSafeDataCollectionBuildingProp(SafeDataCollectionBuildingListActivity.this, bundle);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        isHold=false;
        LoggerHelper.error(getClass(),"继续");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LoggerHelper.error(getClass(),"onPause");
        isHold=true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        isHold=true;
        LoggerHelper.error(getClass(),"挂起");
    }
}
